package com.goldgov.addressbook.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goldgov.addressbook.dao.AddressBookGroupUserDao;
import com.goldgov.addressbook.dao.AddressUserDao;
import com.goldgov.addressbook.service.AddressBookGroupUser;
import com.goldgov.addressbook.service.AddressBookGroupUserQuery;
import com.goldgov.addressbook.service.AddressBookGroupUserService;
import com.goldgov.addressbook.service.AddressUser;
import com.goldgov.addressbook.service.AddressUserQuery;

@Service
public class AddressBookGroupUserServiceImpl implements AddressBookGroupUserService{

	@Autowired 
	private AddressBookGroupUserDao addressBookGroupUserDao;
	@Autowired
	private AddressUserDao addressUserDao;

	@Override
	public List<AddressBookGroupUser> getAddressBookGroupUserList(AddressBookGroupUserQuery query) {
		return addressBookGroupUserDao.getAddressBookGroupUserList(query);
	}

	@Override
	public void addAddressBookGroupUser(AddressBookGroupUser addressBookGroupUser) {
		addressBookGroupUserDao.addAddressBookGroupUser(addressBookGroupUser);
	}

	@Override
	public void deleteAddressBookGroupUser(String groupID) {
		addressBookGroupUserDao.deleteAddressBookGroupUser(groupID);
	}

	@Override
	public List<AddressBookGroupUser> getOrgUserList(AddressBookGroupUserQuery query) {
		return addressBookGroupUserDao.getOrgUserList(query);
	}

	@Override
	public Boolean addAddressUser(AddressUser addressUser) {
		Boolean fase = false;
		List<AddressUser> addressUserList = new ArrayList<>();
		if (addressUser.getUserIDs() != null && addressUser.getUserIDs().length > 0) {
			for (String userId : addressUser.getUserIDs()) {
				AddressUser au = new AddressUser();
				au.setAddressUserID(UUID.randomUUID().toString());
				au.setClassifyID(addressUser.getClassifyID());
				if (addressUser.getClassifyName() != null && !"".equals(addressUser.getClassifyName())) {
					au.setClassifyName(addressUser.getClassifyName());
				}
				au.setUserId(userId);
				addressUserList.add(au);
			}
		}
		if (!addressUserList.isEmpty()) {
			int state = addressUserDao.addAddressUser(addressUserList);
			if (state >= 1) {
				fase = true;
			}
		}else {
			fase = true;
		}
		return fase;
	}

	@Override
	public Boolean deleteAddressUser(String[] ids) {
		Boolean fase = false;
		int state = addressUserDao.deleteAddressUser(ids);
		if (state >= 1) {
			fase = true;
		}
		return fase;
	}

	@Override
	public List<AddressUser> findAddressUser(AddressUserQuery query) {
		return addressUserDao.findAddressUser(query);
	}

	@Override
	public List<String> getExistUserIds(String classifyID) {
		return addressUserDao.getExistUserIds(classifyID);
	}
	
	

}
